package com.renshenlianzhuangyuan.h5;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;

import java.util.Stack;

/**
 * Ӧ�ó���������?<br>
 * ������Ŀ����д���͹رռ�ϵͳ�˳�?
 * 
 * @version 1.0 2014-03-22
 * @author Ц�Ǻ�
 * 
 */
public class AppMgr {
	/**
	 * Activity�洢��
	 */
	private Stack<Activity> store;
	/**
	 * ����
	 */
	private static AppMgr instance;
	static {
		instance = new AppMgr();
		instance.store = new Stack<Activity>();
	}

	/**
	 * ����ģʽ
	 * 
	 * @return
	 */
	public static AppMgr getInstance() {
		return instance;
	}

	/**
	 * Activity����
	 * 
	 * @return
	 */
	public int getActSize() {
		return store.size();
	}

	/**
	 * ���һ��Activity
	 * 
	 * @param ac
	 */
	public void addApp(Activity ac) {
		instance.store.add(ac);
	}

	/**
	 * ���һ��Activity
	 * 
	 * @return
	 */
	public Activity currentApp() {
		return instance.store.lastElement();
	}

	/**
	 * �������һ��Activity
	 */
	public void finishApp() {
		Activity ac = instance.store.lastElement();
		finishApp(ac);
	}

	/**
	 * ����һ��Activity
	 * 
	 * @param ac
	 */
	public void finishApp(Activity ac) {
		if (ac != null) {
			instance.store.remove(ac);
			ac.finish();
			ac = null;
		}
	}

	/**
	 * �������е�Activity
	 */
	public void finishAll() {
		for (int i = 0, size = instance.store.size(); i < size; i++) {
			if (null != instance.store.get(i)) {
				instance.store.get(i).finish();
			}
		}
		instance.store.clear();
	}

	/**
	 * �����˳�
	 */
	public void appExit(Context context) {
		try {
			finishAll();
			ActivityManager activityMgr = (ActivityManager) context
					.getSystemService(Context.ACTIVITY_SERVICE);
			activityMgr.killBackgroundProcesses(context.getPackageName());
			System.exit(0);
		} catch (Exception e) {
		}
	}

}
